package system.utils;

import framework.utils.CharsetUtil;
import lombok.SneakyThrows;
import org.apache.commons.io.FilenameUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * Mime Util
 */
public class MimeUtil {

    /**
     * 获取常见文件媒体类型
     *
     * @param extension
     * @return no match return null
     */
    public static String getMime(String extension) {
        if (extension == null) return null;
        if (extension.startsWith(".")) extension.substring(1);
        String mime;
        //
        mime = getMimeTypeOfMedia(extension);
        if (mime != null) return mime;
        //
        mime = getMimeTypeOfPicture(extension);
        if (mime != null) return mime;
        //
        mime = getMimeTypeOfOffice(extension);
        if (mime != null) return mime;
        //
        return null;
    }

    /**
     * 获取图像媒体类型
     *
     * @param extension
     * @return no match return null
     */
    public static String getMimeTypeOfPicture(String extension) {
        if (extension == null) return null;
        if (extension.startsWith(".")) extension.substring(1);
        switch (extension.toLowerCase()) {
            case "jpeg":
            case "jpg":
                return "image/jpeg";
            case "png":
                return "image/png";
            case "gif":
                return "image/gif";
            case "bmp":
                return "image/bmp";
            case "webp":
                return "image/webp";
            default:
                return null;
//                return "application/octet-stream";
        }
    }

    /**
     * 获取常见办公软件媒体类型
     *
     * @param extension
     * @return no match return null
     */
    public static String getMimeTypeOfOffice(String extension) {
        if (extension == null) return null;
        if (extension.startsWith(".")) extension.substring(1);
        switch (extension.toLowerCase()) {
            case "doc":
            case "docx":
                return "application/msword";
            case "xls":
            case "xlsx":
                return "application/vnd.ms-excel";
            case "ppt":
            case "pptx":
                return "application/vnd.ms-powerpoint";
            case "pdf":
                return "application/pdf";
            case "txt":
                return "text/plain";
            default:
                return null;
//                return "application/octet-stream";
        }
    }

    /**
     * 获取常见影音媒体类型
     *
     * @param extension
     * @return no match return null
     */
    public static String getMimeTypeOfMedia(String extension) {
        if (extension == null) return null;
        if (extension.startsWith(".")) extension.substring(1);
        switch (extension.toLowerCase()) {
            case "mp4":
                return "video/mp4";
            case "avi":
                return "video/x-msvideo";
            case "mkv":
                return "video/x-matroska";
            case "mov":
                return "video/quicktime";
            case "flv":
                return "video/x-flv";
            case "wmv":
                return "video/x-ms-wmv";
            case "mp3":
                return "audio/mpeg";
            case "wav":
                return "audio/x-wav";
            case "ogg":
                return "audio/ogg";
            default:
//                return "application/octet-stream";
                return null;
        }
    }

    /**
     * 设置响应对象的mime
     *
     * @param response
     * @param extension
     */
    public static void setResponseMime(HttpServletResponse response, String extension) {
        String mime = getMime(extension);
        if (mime != null) {
            response.setContentType(mime);
        }
    }

    /**
     * 设置响应为下载
     *
     * @param request
     * @param response
     */
    @SneakyThrows
    public static void setDisposition(HttpServletRequest request, HttpServletResponse response) {
        boolean view = "1".equals(request.getParameter("view")) || "true".equals(request.getParameter("view"));
        if (!view) {
            String filename = FilenameUtils.getName(request.getServletPath());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, CharsetUtil.getSystemCharset()) + "\"");
        }
    }

    /**
     * 设置响应为下载
     *
     * @param request
     * @param response
     * @param filename
     */
    @SneakyThrows
    public static void setDisposition(HttpServletRequest request, HttpServletResponse response, String filename) {
        boolean view = "1".equals(request.getParameter("view")) || "true".equals(request.getParameter("view"));
        if (!view) {
            response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, CharsetUtil.getSystemCharset()) + "\"");
        }
    }

}
